﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BusinessService.Plugin.Entity;
using SqlSugar;
using IFM.Core;
using WindowsForm.FormDtos;
using Microsoft.Extensions.DependencyInjection;
using winform分层框架;
using EMSSevice;
using BusinessService.Plugin.DataSource.Sqlite;
using System.Net.Sockets;
using BusinessService.Plugin.ProtocolMethod;
using BusinessService.Plugin.Services;

namespace WindowsForm.Forms
{
    public partial class DataSetting : UserControl
    {
        private BackService _backService => Program.IOC.GetRequiredService<BackService>();
        private SqliteLibrary _repSqlite => Program.IOC.GetRequiredService<SqliteLibrary>();

        public DataSetting()
        {
            InitializeComponent();
            _repSqlite.UpdateEvent += UpdateTable;

        }


        /// <summary>
        /// 表格刷新事件
        /// </summary>
        public static Action RefreshTable;
        private void DataSetting_Load(object sender, EventArgs e)
        {
            UpdateTable();
            RefreshTable += UpdateTable;
        }
        /// <summary>
        /// 刷新数据表
        /// </summary>
        private void UpdateTable()
        {
            var result = _repSqlite.sqliteQuery();

            var table = result.Select(x => new ParameterSettingsDto
            {
                ID = x.ID,
                Name = x.Name,
                Link = x.Link,
                ProtocolType = x.V_ProtocolType,
                CreateTime = x.CreateTime,


                Btns = new AntdUI.CellLink[] {
                    new AntdUI.CellButton("edit","编辑",AntdUI.TTypeMini.Primary) {  Ghost = true,BorderWidth=1 },
                    new AntdUI.CellButton("delete","删除",AntdUI.TTypeMini.Error) {  Ghost = true ,BorderWidth=1}
                    },

            }).ToList();

            table1.DataSource = table;
            var _btns = new AntdUI.CellLink[] {
                    new AntdUI.CellButton("edit","Edit",AntdUI.TTypeMini.Primary) {  Ghost = true,BorderWidth=1 },
                    new AntdUI.CellButton("delete","Delete",AntdUI.TTypeMini.Error) {  Ghost = true ,BorderWidth=1}
                    };
            table1.Columns = new AntdUI.Column[] {
                      new AntdUI.Column("ID", "ID"),

                      new AntdUI.Column("Name","数据源名称"),
                     new AntdUI.Column("ProtocolType","协议类型"),

                      new AntdUI.Column("Link", "连接对象") ,
                      new AntdUI.Column("CreateTime", "创建时间"),
                      new AntdUI.Column("Btns", "操作") ,

            };// 添加表头，绑定模型名称

        }




        private void button1_Click(object sender, EventArgs e)
        {
            DialogBox dialogBox = new DialogBox();
            dialogBox.ShowDialog();


        }

        private void table1_CellClick(object sender, MouseEventArgs args, object record, int rowIndex, int columnIndex, Rectangle rect)
        {

        }



        private void button3_Click(object sender, EventArgs e)
        {
            UpdateTable();
        }




        private void table1_CellButtonClick(object sender, AntdUI.CellLink btn, MouseEventArgs args, object record, int rowIndex, int columnIndex)
        {


            if (SqlFunc.IsNullOrEmpty(btn)) return;

            if (btn.Text.Equals("删除"))
            {
                var selectRow = (ParameterSettingsDto)record;
                if (selectRow != null)
                {

                    var dialogResult = MessageBox.Show(
                        "确定删除此数据源吗？",
                        " 警告",
                        MessageBoxButtons.OKCancel,
                        MessageBoxIcon.Warning);
                    if (dialogResult == DialogResult.Cancel) return;

                    _repSqlite.sqliteDelete(selectRow.ID);//删除指定行
                    UpdateTable();//刷新表格内容
                };
            }

            if (btn.Text.Equals("编辑"))
            {

                DialogBox dialogBox = new DialogBox();
                var selectRow = (ParameterSettingsDto)record;
                dialogBox.Tag = selectRow.ID;
                dialogBox.ShowDialog();
            }
        }

        private async void button2_Click(object sender, EventArgs e)
        {
            await _backService.PostDataAsync();
        }

        private async void button4_Click(object sender, EventArgs e)
        {
            var rr = await ProtocolClass.HttpGetAsync("http://202.104.182.126:38008/api/EMS/Cockpit/EnergyType",

                "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjE0MjMwNzA3MDkxMDU1MSwiVGVuYW50SWQiOjE0MjMwNzA3MDkxODc4MCwiQWNjb3VudCI6InN1cGVyQWRtaW4iLCJOYW1lIjoi6LaF57qn566h55CG5ZGYIiwiU3VwZXJBZG1pbiI6MSwiVGVuYW50VHlwZSI6MSwiVGVuYW50TmFtZSI6Iuezu-e7n-enn-aItyIsIkVmZmVjdGl2ZVR5cGUiOjEsIkVmZmVjdGl2ZVRpbWUiOiIyMDk5LTEyLTMxVDIzOjU5OjU5IiwiaWF0IjoxNzI2NzEyNDYwLCJuYmYiOjE3MjY3MTI0NjAsImV4cCI6MTcyNjc5ODg2MCwiaXNzIjoiaWZtIiwiYXVkIjoiaWZtIn0.0vOJlJnOLEi1OKhD4ZW7uMahlV5jZLZaNXHu6nmE7hU");
        }
    }


}
